package xyz.lwm.lazycat.utility;

import xyz.lwm.lazycat.utility.exception.UtilException;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author lwm
 */
public abstract class Assert {

    public static <T> void isTrue(Predicate<T> predicate, T t, String fmt, Object... params) {
        isTrue(Objects.requireNonNull(predicate).test(t), fmt, params);
    }

    /**
     * 判断是否符合条件
     *
     * @param expression 表达式
     * @param fmt        信息格式, 比如: Hi, {}.
     * @param params     参数
     */
    public static void isTrue(boolean expression, String fmt, Object... params) {
        if (!expression) {
            throw new UtilException(StringUtil.format(fmt, params));
        }
    }

    public static void notNull(Object o, String name) {
        isTrue(o != null, "{} cannot be null", name);
    }

    public static void notEmpty(CharSequence cs, String name) {
        isTrue(cs != null && cs.length() > 0, "{} cannot be empty", name);
    }

    public static void notEmpty(Collection<?> collection, String name) {
        isTrue(collection != null && !collection.isEmpty(), "{} cannot be empty", name);
    }

    public static void notEmpty(Map<?, ?> map, String name) {
        isTrue(map != null && !map.isEmpty(), "{} cannot be empty", name);
    }

    public static <T> void notEmpty(T[] array, String name) {
        isTrue(array != null && array.length > 0 && array[0] != null, "{} cannot be empty", name);
    }

}
